import BaseVenueProcessor from './BaseVenueProcessor.js';
import { getJinManGuanHuaGongXiVenueSettings } from '../request.js';

/**
 * 金满贯(华工西)网球场地数据处理器
 */
export class JinManGuanHuaGongXiProcessor extends BaseVenueProcessor {
  constructor() {
    super();
    this.venueName = '金满贯(华工西)';
  }

  /**
   * 获取金满贯(华工西)网球场地预约设置
   * @param {Object} params - 查询参数 { startDate, endDate }
   * @returns {Promise} 请求Promise
   */
  async getVenueSettings(params = {}) {
    // 设置默认参数
    const today = new Date();
    const tomorrow = new Date(today);
    tomorrow.setDate(today.getDate() + 1);
    
    const defaultParams = {
      startDate: today,
      endDate: tomorrow
    };
    
    // 合并参数
    const requestData = { ...defaultParams, ...params };
    
    return await getJinManGuanHuaGongXiVenueSettings(requestData);
  }

  /**
   * 验证数据是否有效
   * @param {Object} data - 原始场地数据
   * @returns {boolean} 数据是否有效
   */
  validateData(data) {
    return data && 
           data.data && 
           data.data.data && 
           data.data.data.courtList && 
           Array.isArray(data.data.data.courtList);
  }

  /**
   * 按场地名称分组
   * @param {Object} data - 原始场地数据
   * @returns {Object} 场地映射
   */
  groupVenues(data) {
    const venueMap = {};
    
    data.data.data.courtList.forEach(court => {
      const venueName = `${court.number}号场`;
      if (!venueMap[venueName]) {
        venueMap[venueName] = {
          name: venueName,
          timeSlots: [],
          dataSource: this.venueName
        };
      }
    });
    
    return venueMap;
  }

  /**
   * 处理时间段数据
   * @param {Object} venueMap - 场地映射
   * @param {Object} data - 原始数据
   */
  processTimeSlots(venueMap, data) {
    // 定义时间段映射 (7:00-8:00 到 21:00-22:00)
    const timeSlotLabels = [
      '7:00-8:00', '8:00-9:00', '9:00-10:00', '10:00-11:00', '11:00-12:00',
      '12:00-13:00', '13:00-14:00', '14:00-15:00', '15:00-16:00', '16:00-17:00',
      '17:00-18:00', '18:00-19:00', '19:00-20:00', '20:00-21:00', '21:00-22:00'
    ];

    data.data.data.courtList.forEach(court => {
      const venueName = `${court.number}号场`;
      
      if (court.list && Array.isArray(court.list)) {
        court.list.forEach((slot, index) => {
          // 使用索引映射到固定时间段，忽略API返回的startTime和endTime
          if (index < timeSlotLabels.length) {
            const timeSlot = {
              time: timeSlotLabels[index],
              price: slot.price ? (slot.price / 100).toFixed(0) : '0', // 价格除以100
              available: slot.status === 0, // status为0表示可预订
              id: slot.id || '',
              originalData: slot
            };
            
            if (venueMap[venueName]) {
              venueMap[venueName].timeSlots.push(timeSlot);
            }
          }
        });
      }
    });
  }

  /**
   * 转换为场地列表格式
   * @param {Object} venueMap - 场地映射
   * @returns {Array} 场地列表
   */
  convertToVenueList(venueMap) {
    return Object.values(venueMap).map(venue => ({
      name: venue.name,
      timeSlots: venue.timeSlots,
      dataSource: venue.dataSource
    }));
  }

  /**
   * 处理场地数据
   * @param {Object} data - 原始场地数据
   * @param {string} selectedDate - 选择的日期
   * @returns {Array} 处理后的场地列表
   */
  process(data, selectedDate) {
    if (!this.validateData(data)) {
      console.warn('金满贯(华工西)数据验证失败:', data);
      return [];
    }

    const venueMap = this.groupVenues(data);
    this.processTimeSlots(venueMap, data);
    return this.convertToVenueList(venueMap);
  }
}

export default JinManGuanHuaGongXiProcessor;